

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>bartpy.tree &mdash; BartPy 0.0.1 documentation</title>
  

  
  
  
  

  

  
  
    

  

  
    <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 

  
  <script src="../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../index.html" class="icon icon-home"> BartPy
          

          
          </a>

          
            
            
              <div class="version">
                0.0.1
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../node.html">Node</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../split.html">Split</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tree.html">Tree</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../model.html">Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../sampling.html">Samplers</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">BartPy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>bartpy.tree</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for bartpy.tree</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">List</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">bartpy.mutation</span> <span class="k">import</span> <span class="n">TreeMutation</span>
<span class="kn">from</span> <span class="nn">bartpy.node</span> <span class="k">import</span> <span class="n">TreeNode</span><span class="p">,</span> <span class="n">LeafNode</span><span class="p">,</span> <span class="n">DecisionNode</span>


<div class="viewcode-block" id="Tree"><a class="viewcode-back" href="../../tree.html#bartpy.tree.Tree">[docs]</a><span class="k">class</span> <span class="nc">Tree</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An encapsulation of the structure of a single decision tree</span>
<span class="sd">    Contains no logic, but keeps track of 4 different kinds of nodes within the tree:</span>
<span class="sd">      - leaf nodes</span>
<span class="sd">      - decision nodes</span>
<span class="sd">      - splittable leaf nodes</span>
<span class="sd">      - prunable decision nodes</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    nodes: List[Node]</span>
<span class="sd">        All nodes contained in the tree, i.e. decision and leaf nodes</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nodes</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">]):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_nodes</span> <span class="o">=</span> <span class="n">nodes</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cache_up_to_date</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_prediction</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros_like</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">_split</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">_y</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">nodes</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of all nodes contained in the tree</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nodes</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">leaf_nodes</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">LeafNode</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of all of the leaf nodes in the tree</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nodes</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">is_leaf_node</span><span class="p">()]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">splittable_leaf_nodes</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">LeafNode</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of all leaf nodes in the tree which can be split in a non-degenerate way</span>
<span class="sd">        i.e. not all rows of the covariate matrix are duplicates</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaf_nodes</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">is_splittable</span><span class="p">()]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">decision_nodes</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">DecisionNode</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of decision nodes in the tree.</span>
<span class="sd">        Decision nodes are internal split nodes, i.e. not leaf nodes</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nodes</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">is_decision_node</span><span class="p">()]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">prunable_decision_nodes</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">DecisionNode</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of decision nodes in the tree that are suitable for pruning</span>
<span class="sd">        In particular, decision nodes that have two leaf node children</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">decision_nodes</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">is_prunable</span><span class="p">()]</span>

<div class="viewcode-block" id="Tree.update_y"><a class="viewcode-back" href="../../tree.html#bartpy.tree.Tree.update_y">[docs]</a>    <span class="k">def</span> <span class="nf">update_y</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Update the cached value of the target array in all nodes</span>
<span class="sd">        Used to pass in the residuals from the sum of all of the other trees</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cache_up_to_date</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">:</span>
            <span class="n">node</span><span class="o">.</span><span class="n">split</span><span class="o">.</span><span class="n">update_y</span><span class="p">(</span><span class="n">y</span><span class="p">)</span></div>

<div class="viewcode-block" id="Tree.predict"><a class="viewcode-back" href="../../tree.html#bartpy.tree.Tree.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generate a set of predictions with the same dimensionality as the target array</span>
<span class="sd">        Note that the prediction is from one tree, so represents only (1 / number_of_trees) of the target</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_up_to_date</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prediction</span>
        <span class="k">for</span> <span class="n">leaf</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaf_nodes</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_prediction</span><span class="p">[</span><span class="n">leaf</span><span class="o">.</span><span class="n">split</span><span class="o">.</span><span class="n">condition</span><span class="p">()]</span> <span class="o">=</span> <span class="n">leaf</span><span class="o">.</span><span class="n">predict</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cache_up_to_date</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prediction</span></div>

<div class="viewcode-block" id="Tree.out_of_sample_predict"><a class="viewcode-back" href="../../tree.html#bartpy.tree.Tree.out_of_sample_predict">[docs]</a>    <span class="k">def</span> <span class="nf">out_of_sample_predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Prediction for a covariate matrix not used for training</span>

<span class="sd">        Note that this is quite slow</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        X: pd.DataFrame</span>
<span class="sd">            Covariates to predict for</span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        np.ndarray</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">prediction</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">X</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">leaf</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">leaf_nodes</span><span class="p">:</span>
            <span class="n">prediction</span><span class="p">[</span><span class="n">leaf</span><span class="o">.</span><span class="n">split</span><span class="o">.</span><span class="n">out_of_sample_condition</span><span class="p">(</span><span class="n">X</span><span class="p">)]</span> <span class="o">=</span> <span class="n">leaf</span><span class="o">.</span><span class="n">predict</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">prediction</span></div>

<div class="viewcode-block" id="Tree.remove_node"><a class="viewcode-back" href="../../tree.html#bartpy.tree.Tree.remove_node">[docs]</a>    <span class="k">def</span> <span class="nf">remove_node</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">:</span> <span class="n">TreeNode</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Remove a single node from the tree</span>
<span class="sd">        Note that this is non-recursive, only drops the node and not any children</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_nodes</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">node</span><span class="p">)</span></div>

<div class="viewcode-block" id="Tree.add_node"><a class="viewcode-back" href="../../tree.html#bartpy.tree.Tree.add_node">[docs]</a>    <span class="k">def</span> <span class="nf">add_node</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">:</span> <span class="n">TreeNode</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Add a node to the tree</span>
<span class="sd">        Note that this is non-recursive, only adds the node and not any children</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_nodes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="mutate"><a class="viewcode-back" href="../../tree.html#bartpy.tree.mutate">[docs]</a><span class="k">def</span> <span class="nf">mutate</span><span class="p">(</span><span class="n">tree</span><span class="p">:</span> <span class="n">Tree</span><span class="p">,</span> <span class="n">mutation</span><span class="p">:</span> <span class="n">TreeMutation</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Apply a change to the structure of the tree</span>
<span class="sd">    Modifies not only the tree, but also the links between the TreeNodes</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    tree: Tree</span>
<span class="sd">        The tree to mutate</span>
<span class="sd">    mutation: TreeMutation</span>
<span class="sd">        The mutation to apply to the tree</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">tree</span><span class="o">.</span><span class="n">cache_up_to_date</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">if</span> <span class="n">mutation</span><span class="o">.</span><span class="n">kind</span> <span class="o">==</span> <span class="s2">&quot;prune&quot;</span><span class="p">:</span>
        <span class="n">tree</span><span class="o">.</span><span class="n">remove_node</span><span class="p">(</span><span class="n">mutation</span><span class="o">.</span><span class="n">existing_node</span><span class="p">)</span>
        <span class="n">tree</span><span class="o">.</span><span class="n">remove_node</span><span class="p">(</span><span class="n">mutation</span><span class="o">.</span><span class="n">existing_node</span><span class="o">.</span><span class="n">left_child</span><span class="p">)</span>
        <span class="n">tree</span><span class="o">.</span><span class="n">remove_node</span><span class="p">(</span><span class="n">mutation</span><span class="o">.</span><span class="n">existing_node</span><span class="o">.</span><span class="n">right_child</span><span class="p">)</span>
        <span class="n">tree</span><span class="o">.</span><span class="n">add_node</span><span class="p">(</span><span class="n">mutation</span><span class="o">.</span><span class="n">updated_node</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">mutation</span><span class="o">.</span><span class="n">kind</span> <span class="o">==</span> <span class="s2">&quot;grow&quot;</span><span class="p">:</span>
        <span class="n">tree</span><span class="o">.</span><span class="n">remove_node</span><span class="p">(</span><span class="n">mutation</span><span class="o">.</span><span class="n">existing_node</span><span class="p">)</span>
        <span class="n">tree</span><span class="o">.</span><span class="n">add_node</span><span class="p">(</span><span class="n">mutation</span><span class="o">.</span><span class="n">updated_node</span><span class="o">.</span><span class="n">left_child</span><span class="p">)</span>
        <span class="n">tree</span><span class="o">.</span><span class="n">add_node</span><span class="p">(</span><span class="n">mutation</span><span class="o">.</span><span class="n">updated_node</span><span class="o">.</span><span class="n">right_child</span><span class="p">)</span>
        <span class="n">tree</span><span class="o">.</span><span class="n">add_node</span><span class="p">(</span><span class="n">mutation</span><span class="o">.</span><span class="n">updated_node</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">tree</span><span class="o">.</span><span class="n">nodes</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">right_child</span> <span class="o">==</span> <span class="n">mutation</span><span class="o">.</span><span class="n">existing_node</span><span class="p">:</span>
            <span class="n">node</span><span class="o">.</span><span class="n">_right_child</span> <span class="o">=</span> <span class="n">mutation</span><span class="o">.</span><span class="n">updated_node</span>
        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">left_child</span> <span class="o">==</span> <span class="n">mutation</span><span class="o">.</span><span class="n">existing_node</span><span class="p">:</span>
            <span class="n">node</span><span class="o">.</span><span class="n">_left_child</span> <span class="o">=</span> <span class="n">mutation</span><span class="o">.</span><span class="n">updated_node</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Jake Coltman.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../',
            VERSION:'0.0.1',
            LANGUAGE:'None',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../../_static/js/theme.js"></script>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>